import os
import re
import shutil
from curl_cffi import requests
import time


root = '../Static/netbian'
if os.path.exists(root):
    shutil.rmtree(root)
    os.makedirs(root)
url = 'https://pic.netbian.com'
headers = {
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
    'Referer': 'https://pic.netbian.com/'
}

headers1 = {
    'referer': 'https://pic.netbian.com/index.html',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36'
}

response = requests.get(url, headers=headers)

content = response.content.decode("gbk")
datas = re.findall(r'<a href="(.*?)" title="(.*?)" target="_blank"><span><img src="(.*?)" .*?</b></a>', content)
for data in datas:
    time.sleep(3)
    # print(data)
    detail_url = data[0]
    thumb_title = data[1]
    thumb_url = f'{url}{data[2]}'
    print(thumb_url)
    thumb_img = requests.get(thumb_url, headers=headers1)
    # print(thumb_img.content[:200])
    with open(f'../Static/netbian/{thumb_title}_thumb.jpg', 'wb') as f:
       f.write(thumb_img.content)
       print(f'{thumb_title}下载完成')